<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/6/12
 * Time: 8:38 PM
 * To change this template use File | Settings | File Templates.
 */
// no direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

class VastModelCongtacs extends JModelList
{
    /**
     * Constructor.
     *
     * @param	array	An optional associative array of configuration settings.
     * @see		JController
     * @since	1.6
     */
    public function __construct($config = array())
    {
        if (empty($config['filter_fields'])) {
            $config['filter_fields'] = array(
                'id', 'a.id',
            );
        }

        parent::__construct($config);
    }

    /**
     * Method to auto-populate the model state.
     *
     * Note. Calling getState in this method will result in recursion.
     *
     * @return	void
     * @since	1.6
     */
    protected function populateState($ordering = null, $direction = null)
    {
        // List state information.
        //Load the filter state
        $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
        $this->setState('filter.search', $search);

        $state = $this->getUserStateFromRequest($this->context . '.filter.state', 'filter_state');
        $this->setState('filter.state', $state);

        //Load the filter state
        $search = $this->getUserStateFromRequest($this->context . '.filter.donvi', 'filter_donvi');
        $this->setState('filter.donvi', $search);

        //Load the filter state
        $search = $this->getUserStateFromRequest($this->context . '.filter.phong', 'filter_phong');
        $this->setState('filter.phong', $search);

        //Load the parameters.
        $params = JComponentHelper::getParams('com_vast');
        $this->setState('params', $params);

        parent::populateState('a.id', 'asc');
    }

    /**
     * Build an SQL query to load the list data.
     *
     * @return	JDatabaseQuery
     * @since	1.6
     */
    protected function getListQuery()
    {
        // Create a new query object.
        $db		= $this->getDbo();
        $query	= $db->getQuery(true);

        $query->select('a.*, h.hoten, t.mota, d.ten as tendonvi, p.ten as tenphong');
        $query->from('#__congtac AS a');
        $query->innerJoin('#__hosocanbo as h ON a.hosocanbo_id = h.id');
        $query->innerJoin('#__tinhtranglamviec as t ON t.id = a.tinhtranglamviec_id');
        $query->leftJoin('#__donvi as d ON d.id = a.donvi_id');
        $query->leftJoin('#__phong as p ON p.id = a.phong_id');

        //Filter
        $search = $this->state->get('filter.search');
        if (!empty($search))
        {
            $query->where('(h.hoten LIKE \'%' . $search . '%\')');
        }

        $search = $this->state->get('filter.donvi');

        if (!empty($search) && ($search != 0))
        {
            $query->where('(a.donvi_id = ' . $search . ')');
        }

        $search = $this->state->get('filter.phong');

        if (!empty($search) && ($search != 0))
        {
            $query->where('(a.phong_id = ' . $search . ')');
        }

        // Add the list ordering clause.
        $orderCol	= $this->state->get('list.ordering', 'a.id');
        $orderDirn	= $this->state->get('list.direction', 'asc');
        $query->order($db->escape($orderCol.' '.$orderDirn));

        // echo nl2br(str_replace('#__','jos_',$query));
        return $query;
    }

    public function getDonvis()
    {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__donvi AS a');

        $db->setQuery($query);
        $column = $db->loadObjectList();

        return (array)$column;
    }
    public function getPhongs()
    {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__phong AS a');

        $db->setQuery($query);
        $column = $db->loadObjectList();

        return (array)$column;
    }
}